﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Collections.ObjectModel;

namespace Wimat.Collections.Tests
{
    [TestFixture]
    public class RandomizedCollectionViewTests
    {
        [Test]
        public void RandomizeTest()
        {
            var source = new ObservableCollection<int>();

            var rcv = new RandomizedCollectionView(source)
            {
                RandomizedExpectedCount = 3,
            };
            var randomized = rcv.Randomized;

            Assert.IsNotNull(randomized);
            Assert.AreEqual(0, randomized.Count);

            //behavior during adding
            source.Add(1);
            rcv.Randomize();
            Assert.AreEqual(1, randomized.Count);

            source.Add(2);
            rcv.Randomize();
            Assert.AreEqual(2, randomized.Count);

            source.Add(3);
            rcv.Randomize();
            Assert.AreEqual(3, randomized.Count);

            source.Add(4);
            rcv.Randomize();
            Assert.AreEqual(3, randomized.Count);

            source.Add(5);
            rcv.Randomize();
            Assert.AreEqual(3, randomized.Count);

            //behavior during removing
            source.Remove(1);
            rcv.Randomize();
            Assert.AreEqual(3, randomized.Count);

            source.Remove(3);
            rcv.Randomize();
            Assert.AreEqual(3, randomized.Count);

            source.Remove(5);
            rcv.Randomize();
            Assert.AreEqual(2, randomized.Count);

            source.Remove(4);
            rcv.Randomize();
            Assert.AreEqual(1, randomized.Count);

            //...and adding again
            source.Add(4);
            rcv.Randomize();
            Assert.AreEqual(2, randomized.Count);

            //and just randomizing
            rcv.Randomize();
            Assert.AreEqual(2, randomized.Count);
            rcv.Randomize();
            Assert.AreEqual(2, randomized.Count);
            rcv.Randomize();
            Assert.AreEqual(2, randomized.Count);
        }
        [Test]
        public void MoveCurrentToRandomTest()
        {
            var source = new int[] { 0, 1, 2 };

            var rcv = new RandomizedCollectionView(source);

            var resultCounts = new int[3];

            for (int i = 0; i < 100; i++)
            {
                rcv.MoveCurrentToRandom();
                int current = (int)rcv.CurrentItem;
                resultCounts[current]++;
            }
            Assert.AreNotEqual(0, resultCounts[0]);
            Assert.AreNotEqual(0, resultCounts[1]);
            Assert.AreNotEqual(0, resultCounts[2]);
        }
    }
}
